import logging

import pymysql


def DB_Unit_link(sql):
    conn = pymysql.connect(host='121.43.169.97', port=3306,
                           user='root', password='Itcast_p2p_20191228',
                           database='czbk_member', charset='utf8')
    cur = conn.cursor()
    try:
        n = cur.execute(sql)
        if sql.split(' ')[0].lower() == 'select':
            logging.info(f'操作的是查询语句，返回的行为 = {n}')
            return print(f'操作的是查询语句，返回的行为 = {n}')
    except  Exception as e:
        conn.rollback()
        print(f'出现异常，异常情况为 = {e}')
        logging.info(f'出现异常，异常情况为 = {e}')
    else:
        conn.commit()
        print(f'操作成功，受影响的行 = {n}')
        logging.info(f'操作成功，受影响的行 = {n}')
    finally:
        cur.close()
        conn.close()

def DB_Unit_qx():
    sql1 = "delete i.* from mb_member_info i inner join mb_member m on m.id = i.member_id where m.phone in ('18814025411');"
    sql2 = "delete l.* from mb_member_login_log l inner join mb_member m on m.id=l.member_id where m.phone in ('18814025411')"
    sql3 = "delete from mb_member where phone in ('18814025411')"
    sql4 = "delete from mb_member_register_log where phone in ('18814025411')"
    DB_Unit_link(sql1)
    DB_Unit_link(sql2)
    DB_Unit_link(sql3)
    DB_Unit_link(sql4)
